<?php
/*
	Version   :  1.0
	Created   :  20000102
	Modified  :  --
	Author    :  Pablo Ram�rez
	co-Author :
	Descr     : Modelo de Entidad "Order Estimates"
	Changelog :
*/
class OrderEstimate extends AppModel
{
	var $name       = 'OrderEstimate';
//	var $id         = 'est_codigo';
	var $primaryKey   = 'est_codigo'; //se define primary key para que haga update de registro y no caiga en duplicate entry
	var $transactional = true;

function getEstimatesByMonth($own_codigo, $from, $to, $sut_codigo) {
		$sut_codigo = $sut_codigo . '%';
		$month = substr($from, 5, 2);
		$output = array();
		$results = $this->query("SELECT count(distinct(e.est_codigo)) as qty, 
										'$month' as month,
										sum(e.est_original) as estimate_initial, 
										sum(e.est_final) as estimate_final
								FROM 	order_estimates e 
								WHERE 	e.ord_codigo in (SELECT o.ord_codigo 
														 FROM orders o 
														 WHERE o.own_codigo = '$own_codigo' 
														 AND o.ord_fechacreacion >= '$from' 
														 AND ord_fechacreacion <= '$to'
														 AND o.sut_codigo LIKE '$sut_codigo') 
								ORDER BY e.ord_codigo");
		
		foreach ($results as $key=>$value) {
			foreach ($value as $key=>$data) {
				foreach ($data as $key=>$info) {
					//pr($key);
					//array_push ($output, $data);
					$output[$key] = $info;	
				}
			}
		}

		return $output;
		
	}


	function getEstimatesByDepot($own_codigo, $dep_codigo, $from, $to, $sut_codigo) {
		$sut_codigo = $sut_codigo . '%';
		$month = substr($from, 5, 2);
		$output = array();
		$results = $this->query("SELECT count(distinct(e.est_codigo)) as qty, 
										'$month' as month,
										sum(e.est_original) as estimate_initial, 
										sum(e.est_final) as estimate_final
								FROM 	order_estimates e 
								WHERE 	e.ord_codigo in (SELECT o.ord_codigo 
														 FROM orders o 
														 WHERE o.own_codigo = '$own_codigo' 
														 AND o.ord_fechacreacion >= '$from' 
														 AND ord_fechacreacion <= '$to'
														 AND o.sut_codigo LIKE '$sut_codigo') 
								AND     e.dep_codigo = '$dep_codigo'
								ORDER BY e.ord_codigo");
		
		foreach ($results as $key=>$value) {
			foreach ($value as $key=>$data) {
				foreach ($data as $key=>$info) {
					$output[$key] = $info;	
				}
			}
		}

		return $output;
			
	}

	function getEstimatesByCountry($own_codigo, $cou_codigo, $from, $to, $sut_codigo, $dep_nombre) {
		$sut_codigo = $sut_codigo . '%';
		$dep_codigo_array = explode(' | ', $dep_nombre);
		//pr($dep_codigo_array);
		$dep_codigo = trim($dep_codigo_array[0]); 
		$output = array();
		$results = $this->query("SELECT count(distinct(e.est_codigo)) as qty, 
										'$dep_nombre' as depot,
										sum(e.est_original) as estimate_initial, 
										sum(e.est_final) as estimate_final
								FROM 	order_estimates e 
								WHERE 	e.ord_codigo in (SELECT o.ord_codigo 
														 FROM orders o 
														 WHERE o.own_codigo = '$own_codigo' 
														 AND o.ord_fechacreacion >= '$from' 
														 AND o.ord_fechacreacion <= '$to'
														 AND o.sut_codigo LIKE '$sut_codigo'
														 AND o.cou_codigo = '$cou_codigo')
								AND e.dep_codigo = '$dep_codigo'
								ORDER BY e.ord_codigo");
		
		foreach ($results as $key=>$value) {
			foreach ($value as $key=>$data) {
				foreach ($data as $key=>$info) {
					$output[$key] = $info;	
				}
			}
		}
	
		return $output;		
	}
}


?>